<template>
  <el-dialog
    append-to-body
    :title="info"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    align="left"
    v-loading="currLoading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    width="700px">
    <el-form v-if="dialogVisible" ref="form" :model="form" label-width="80px" :rules="rules">
      <el-form-item label="快递公司" align="left" prop="expressCompany">
        <el-select v-model="form.expressCompany" placeholder="快递公司">
          <el-option
            v-for="(item,index) in expressCompanyArr"
            :key="index"
            :label="item"
            :value="item">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="快递编号" align="left" prop="expressNumber">
        <el-input v-model="form.expressNumber" style="width: 300px"></el-input>
      </el-form-item>
      <el-form-item label="发货地址" align="left" prop="addressId">
        <address-item :is-re-choose="true" :show-type="1" :choose-address-call-back="setAddress" v-if="address && address.id>0" :address-data="address"/>
        <el-button type="primary" v-if="!(address && address.id>0)" @click="$router.push('/SupplierAddressFormPage?type=create')">暂无地址,添加新地址</el-button>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import AddressItem from '../Common/AddressItem'
export default {
  name: 'OrderSubmitExpressDialog',
  components: {AddressItem},
  props: {
    successFunction: Function
  },
  data: function () {
    let $this=this
    var validateExpressNumber=function (rule, value, callback) {
      if ($this.checkExpressNumber(value)) {
        callback()
      }else{
        callback(new Error('快递单号 必须是由(A-Za-z0-9)组成，长度在5~50位之间'))
      }
    }
    return {
      info: '订单确认发货',
      address:{},
      dialogVisible: false,
      currLoading: false,
      expressCompanyArr: dict.ExpressCompanyList,
      form: {
        id: 0,
        expressCompany: '',
        expressNumber: ''
      },
      rules: {
        expressCompany: [
          {required: true, message: '请输入 快递公司', trigger: ['blur', 'change']},
          {min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: ['blur', 'change']}
        ],
        expressNumber: [
          {required: true, message: '请输入 快递编号', trigger: ['blur', 'change']},
          {validator: validateExpressNumber, trigger: ['blur', 'change']}
        ]
      },
      orderDetail:{}
    }
  },
  mounted () {
  },
  methods: {
    setAddress:function (address) {
      this.address=address
    },
    // 获取-默认地址
    getDefaultAddress: function () {
      var $this = this
      var form={
        isDefault:1
      }
      $this.MyRequest($this.ApiDoNameSupplier + 'SupplierAddress/getList', form, function (data) {
        if (data.length>0){
          $this.address=data[0]
        }
        $this.$forceUpdate()
      }, function (msg) {
        $this.$message.error(msg)
      })
    },
    open: function (order) {
      this.orderDetail = order
      this.getDefaultAddress()
      this.dialogVisible = true
    },
    submit: function () {
      let $this = this
      $this.$refs.form.validate((valid) => {
        if (valid) {
          if (!$this.form.expressCompany) {
            return $this.$message.error('快递公司 不能为空')
          }
          if (!$this.form.expressNumber) {
            return $this.$message.error('快递单号 不能为空')
          }
          if (!$this.address || !$this.address.id) {
            return $this.$message.error('发货地址必须 不能为空')
          }
          $this.$refs.form.validate((valid) => {
            if (valid) {
              var form = {
                id: $this.orderDetail.id,
                expressCompany: $this.form.expressCompany,
                expressNumber: $this.form.expressNumber,
                addressId: $this.address.id,
              }
              $this.currLoading = true
              $this.MyRequest(this.ApiDoNameSupplier + 'SupplierOrder/send', form, function () {
                $this.$message.success('操作成功')
                $this.currLoading = false
                $this.dialogVisible = false
                $this.$props.successFunction()
                $this.$forceUpdate()
              }, function () {
                $this.$message.success('操作失败，请重试')
                $this.currLoading = false
                $this.$forceUpdate()
              })
            } else {
              return false
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
